package com.xiong.wuliao.util;

import com.xiong.wuliao.entity.GoodSpecial;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 单品点击商品进去后的商品详情对象
 * Created by Administrator on 2016/11/13.
 */
public class GoodSepcialJSON {

    public static GoodSpecial saveGoodSpecialData(String s){
        GoodSpecial goodSpecial = new GoodSpecial();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(s.trim());
            JSONObject object = jsonObject.optJSONObject("data");

            goodSpecial.setComments_count(object.optString("comments_count"));
            goodSpecial.setDescription(object.optString("description"));
            goodSpecial.setDetail_html(object.optString("detail_html"));
            goodSpecial.setFavorited(object.optBoolean("favorited"));
            goodSpecial.setFavorites_count(object.optString("favorites_count"));
            goodSpecial.setId(object.optString("id"));

            JSONArray array = object.getJSONArray("image_urls");
            List<String> list;
            if (array != null) {
                list = new ArrayList<>();
                for (int i = 0; i < array.length(); i++) {
                    list.add(array.optString(i));
                }
                goodSpecial.setImage_urls(list);
            }

            goodSpecial.setName(object.optString("name"));
            goodSpecial.setPrice(object.optString("price"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return goodSpecial;
    }
}
